package com.inifitness.message.consumer;

import java.time.LocalDateTime;
import java.util.Map;

import com.inifitness.common.event.event.MessageEvent;
import com.inifitness.common.rabbitmq.EventRabbitConfig;
import com.inifitness.message.context.MessageProcessorContext;
import com.rabbitmq.client.Channel;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

/**
 * 事件消息消费
 *
 * @author sunjinfeng
 * @version 1.0.0
 * @since 2023-02-01 17:18
 */
@Component
@Log4j2
@RequiredArgsConstructor
public class EventConsumer {

  private final MessageProcessorContext messageProcessorContext;

  @RabbitListener(queues = EventRabbitConfig.EVENT_QUEUE)
  @RabbitHandler
  public void process(@Payload MessageEvent event, Channel channel,
      @Headers Map<String, Object> headers) {
    log.info("当前时间：{},接收到消息:{}", LocalDateTime.now(), event);
    messageProcessorContext.sendMessage(event);
  }

}
